package com.tankgame;

import java.util.Vector;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: FENGYiMing
 * @Date: 2024/03/29/17:48
 * @Description:
 */
public class EnemyTank extends Tank implements Runnable {
    Vector<Shot> shots = new Vector<>();

    Vector<EnemyTank> enemyTankList = new Vector<>();

    public EnemyTank(int x, int y, int direct) {
        super(x, y, direct);
    }

    public void setEnemyTankList(Vector<EnemyTank> enemyTankList) {
        this.enemyTankList = enemyTankList;
    }

    public boolean isTouchTank() {
        switch (getDirection()) {
            case 0:
                for (EnemyTank item : enemyTankList) {
                    if (item != this) {
                        if (item.getDirection() == 0 || item.getDirection() == 2) {
                            if (this.getX() >= item.getX() && this.getX() <= item.getX() + 40 && this.getY() >= item.getY() && this.getY() <= item.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 40 >= item.getX() && this.getX() + 40 <= item.getX() + 40 && this.getY() >= item.getY() && this.getY() <= item.getY() + 60) {
                                return true;
                            }
                        }
                        if (item.getDirection() == 1 || item.getDirection() == 3) {
                            if (this.getX() >= item.getX() && this.getX() <= item.getX() + 60 && this.getY() >= item.getY() && this.getY() <= item.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 40 >= item.getX() && this.getX() + 40 <= item.getX() + 60 && this.getY() >= item.getY() && this.getY() <= item.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1:
                for (EnemyTank item : enemyTankList) {
                    if (item != this) {
                        if (item.getDirection() == 0 || item.getDirection() == 2) {
                            if (this.getX() + 60 >= item.getX() && this.getX() + 60 <= item.getX() + 40 && this.getY() >= item.getY() && this.getY() <= item.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 60 >= item.getX() && this.getX() + 60 <= item.getX() + 40 && this.getY() + 40 >= item.getY() && this.getY() + 40 <= item.getY() + 60) {
                                return true;
                            }
                        }
                        if (item.getDirection() == 1 || item.getDirection() == 3) {
                            if (this.getX() + 60 >= item.getX() && this.getX() + 60 <= item.getX() + 60 && this.getY() >= item.getY() && this.getY() <= item.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 60 >= item.getX() && this.getX() + 60 <= item.getX() + 60 && this.getY() + 40 >= item.getY() && this.getY() + 40 <= item.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2:
                for (EnemyTank item : enemyTankList) {
                    if (item != this) {
                        if (item.getDirection() == 0 || item.getDirection() == 2) {
                            if (this.getX() >= item.getX() && this.getX() <= item.getX() + 40 && this.getY() + 60 >= item.getY() && this.getY() + 60 <= item.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 40 >= item.getX() && this.getX() + 40 <= item.getX() + 40 && this.getY() + 60 >= item.getY() && this.getY() + 60 <= item.getY() + 60) {
                                return true;
                            }
                        }
                        if (item.getDirection() == 1 || item.getDirection() == 3) {
                            if (this.getX() >= item.getX() && this.getX() <= item.getX() + 60 && this.getY() + 60 >= item.getY() && this.getY() + 60 <= item.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 40 >= item.getX() && this.getX() + 40 <= item.getX() + 60 && this.getY() + 60 >= item.getY() && this.getY() + 60 <= item.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3:
                for (EnemyTank item : enemyTankList) {
                    if (item != this) {
                        if (item.getDirection() == 0 || item.getDirection() == 2) {
                            if (this.getX() >= item.getX() && this.getX() <= item.getX() + 40 && this.getY() >= item.getY() && this.getY() <= item.getY() + 60) {
                                return true;
                            }
                            if (this.getX() >= item.getX() && this.getX() <= item.getX() + 40 && this.getY() + 40 >= item.getY() && this.getY() + 40 <= item.getY() + 60) {
                                return true;
                            }
                        }
                        if (item.getDirection() == 1 || item.getDirection() == 3) {
                            if (this.getX() >= item.getX() && this.getX() <= item.getX() + 60 && this.getY() >= item.getY() && this.getY() <= item.getY() + 40) {
                                return true;
                            }
                            if (this.getX() >= item.getX() && this.getX() <= item.getX() + 60 && this.getY() + 40 >= item.getY() && this.getY() + 40 <= item.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            default:
                break;

        }

        return false;
    }

    @Override
    public void run() {
        do {
            Shot shot = null;
            switch (getDirection()) {
                case 0:
                    for (int i = 0; i < getMovingDistance(); i++) {
                        if (!isTouchTank()) {
                            moveUp();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    if (isLive()) {
                        shot = new Shot(getX() + 20, getY(), getDirection());
                    }
                    break;
                case 1:
                    for (int i = 0; i < getMovingDistance(); i++) {
                        if (!isTouchTank()) {
                            moveRight();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    if (isLive()) {
                        shot = new Shot(getX() + 60, getY() + 20, getDirection());
                    }
                    break;
                case 2:
                    for (int i = 0; i < getMovingDistance(); i++) {
                        if (!isTouchTank()) {
                            moveDown();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    if (isLive()) {
                        shot = new Shot(getX() + 20, getY() + 60, getDirection());
                    }
                    break;
                case 3:
                    for (int i = 0; i < getMovingDistance(); i++) {
                        if (!isTouchTank()) {
                            moveLeft();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    if (isLive()) {
                        shot = new Shot(getX(), getY() + 20, getDirection());
                    }
                    break;
                default:
                    break;
            }
            setDirection((int) (Math.random() * 4));
            if (shots.size() < 3) {
                shots.add(shot);
                new Thread(shot).start();
            }
        } while (isLive());
    }
}
